<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
use Common\Model\AdModel;
use Common\Model\SysconfigModel;
use Think\Controller;

class ThemController extends  CommonController{


	public function index(){
		$sysModel = new SysconfigModel();
		$template = $sysModel->where("varname = 'DEFAULT_THEME' ")->getField('value');
		$baseDir = 'Template/' . $template .'/*';

		$file = $this->globDir($baseDir);
		$this->assign("data",$file);
		$this->display();
	}


	public function globDir($path){
		$dir = glob($path.'/*.php');
		$filePath = array();
		foreach($dir as $k => $v){
			if(is_dir($v)){
				$path = $this->globDir($v);
				$filePath = array_merge($filePath,$path);
				$filePath[] = $v;
			}else{
				$filePath[] = $v;
			}
		}
		return $filePath;
	}

	public function edit($id){
		$code = file_get_contents($id);
		if(!$code){
			$code = '请确认 文件是否存在';
			$this->error($code);
		}
		if(IS_POST){
			if($_POST['code'] !== $code){
				$res = file_put_contents($id,$_POST['code']);
				$this->success('模板修改成功');
			}
		}
		$this->assign('code',$code)->assign('id',$id);
		$this->display();
	}
}
